package com.xunxi.mimi.ui.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;

import com.xunxi.mimi.app_base.R;


/**
 * activity加载进度条基类
 * liyu
 */
public abstract class BaseLoadingActivity extends BaseActivity{

    protected String TAG = this.getClass().getSimpleName();

    FrameLayout mContentFl;

    private View mLoadingView;

    protected FrameLayout mEmptyView;

    @Override
    protected void onCreate(Bundle savedInstanceState, String tag) {
        super.setContentView(R.layout.activity_base_loading);
        mContentFl = (FrameLayout) findViewById(R.id.content);
        mEmptyView = (FrameLayout) findViewById(R.id.empty_view);
        mLoadingView = findViewById(R.id.loading_content_ll);

        this.onCreateView(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View contentView = getLayoutInflater().inflate(layoutResID, null);
        mContentFl.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mEmptyView.addView(onCreateEmptyView(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void setContentView(View contentView) {
        mContentFl.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void setContentView(@LayoutRes int layoutResID, ViewGroup.LayoutParams params) {
        View contentView = getLayoutInflater().inflate(layoutResID, null);
        mContentFl.addView(contentView, params);
    }

    @Override
    public void setContentView(View contentView, ViewGroup.LayoutParams params) {
        mContentFl.addView(contentView, params);
    }

    /**
     * 显示进度条
     */
    public void dismissLoadingContentView() {
        if (mLoadingView != null){
            mLoadingView.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏进度条
     */
    public void showLoadingContentView() {
        if (mLoadingView != null){
            mLoadingView.setVisibility(View.VISIBLE);
        }
    }

    protected abstract void onCreateView(Bundle savedInstanceState);

    protected View onCreateEmptyView(){
        View emptyView = getLayoutInflater().inflate(R.layout.include_empty_list, null);
        emptyView.setVisibility(View.VISIBLE);
        ImageView emptyImg = (ImageView) emptyView.findViewById(R.id.iv_empty_icon);
        emptyImg.setImageResource(getEmptyIcon());
        TextView emptyTv = (TextView) emptyView.findViewById(R.id.tv_empty_text);
        emptyTv.setText(getEmptyTitle());
        emptyImg.setImageResource(getEmptyIcon());
        return emptyView;
    }

    protected int getEmptyIcon(){
        return R.mipmap.ic_empty_follow;
    }

    protected int getEmptyTitle(){
        return R.string.empty_data;
    }

    protected void showEmptyView(){
        if (mEmptyView != null){
            mEmptyView.setVisibility(View.VISIBLE);
        }
    }

    protected void hideEmptyView(){
        if (mEmptyView != null){
            mEmptyView.setVisibility(View.GONE);
        }
    }
}
